1 00:00:00,570 --> 00:00:01,290 Hey there. 2 00:00:01,290 --> 00:00:06,540 In this lecture, we're now going to be scripting the UI for our guns to display how much ammo is in 3 00:00:06,540 --> 00:00:10,530 our magazines, as well as how much reserve ammo we have left in our gun. 4 00:00:10,530 --> 00:00:16,440 So if I shoot my gun, as you can see, the magazine ammo goes down and then when I reload, it'll display 5 00:00:16,440 --> 00:00:20,490 a message of reloading and then it subtracts that from our reserve ammo. 6 00:00:20,490 --> 00:00:22,020 So let's get started. 7 00:00:24,130 --> 00:00:29,260 Inside of the local script we've been editing for our guns, we can go ahead and grab the guy that's 8 00:00:29,260 --> 00:00:32,290 inside of each one of our guns, which is called our gun guy. 9 00:00:32,650 --> 00:00:38,230 We'll make a variable called Guy, and that's going to be equal to our gun dot gun guy. 10 00:00:38,960 --> 00:00:44,570 And now what we can go ahead and do is when we scroll down and we equip our gun, we want to go ahead 11 00:00:44,570 --> 00:00:50,900 and display it on our screen so we can change the parent of our gun guy to be equal to our players player 12 00:00:50,900 --> 00:00:54,290 guy folder, and that will allow it to render onto the screen. 13 00:00:55,130 --> 00:00:59,480 And then when we unequip our guns, we want to go ahead and put the guy back inside of the gun. 14 00:00:59,480 --> 00:01:06,200 So right here we can go ahead and refer to guy and set the parent back equal to be inside of our tool. 15 00:01:06,530 --> 00:01:12,440 Now in order to display when the ammo in our gun changes, we can go ahead and listen to the attribute 16 00:01:12,440 --> 00:01:18,260 changed event on our guns, which will notify us when either the ammo and our magazine or the ammo reserve 17 00:01:18,260 --> 00:01:23,090 attribute has been updated by the server, and then we can go ahead and display that new value on our 18 00:01:23,090 --> 00:01:23,840 guy. 19 00:01:23,840 --> 00:01:29,600 So we'll refer to our gun, get the attribute changed event, and then we'll connect a function. 20 00:01:29,600 --> 00:01:33,200 And this will get passed the name of the attribute that got changed. 21 00:01:33,200 --> 00:01:34,490 So we'll just call it name. 22 00:01:34,790 --> 00:01:41,840 Then we can go ahead and check to see if the name is equal to our ammo and mag attribute. 23 00:01:41,840 --> 00:01:48,590 If it is, then inside of our GUI and inside of the frame, we have the mag ammo text, and we can go 24 00:01:48,590 --> 00:01:55,040 ahead and update the text inside of there to be equal to something like mag ammo, and then we can go 25 00:01:55,040 --> 00:01:59,120 ahead and concatenate this with the gun's current ammo that's in the magazine. 26 00:01:59,120 --> 00:02:02,330 So we'll do get attribute ammo in mag. 27 00:02:02,960 --> 00:02:08,750 Otherwise we can check if this attribute that changed is the ammo reserve attribute, and if it is, 28 00:02:08,750 --> 00:02:10,760 we can do the exact same thing that we did here. 29 00:02:10,760 --> 00:02:14,540 But this time we want to change the reserve ammo text. 30 00:02:15,520 --> 00:02:20,620 And here we can say something like reserve ammo and then we'll get the attribute. 31 00:02:20,980 --> 00:02:22,540 Ammo reserve. 32 00:02:23,120 --> 00:02:28,160 So now our guy is going to update when either of these two attributes inside of our gun changes, which 33 00:02:28,160 --> 00:02:29,000 is very cool. 34 00:02:30,340 --> 00:02:36,130 Now, something to note is that by default, if I go and move one of my guys into my starter GUI folder 35 00:02:36,130 --> 00:02:38,350 and then let me enable UI visibility. 36 00:02:38,350 --> 00:02:41,200 As you can see, by default the text is blank. 37 00:02:41,200 --> 00:02:47,320 So when this local script inside of our gun first executes, what we want to do is down in this main 38 00:02:47,320 --> 00:02:52,810 section, we want to go ahead and update the text inside of our gun, so we can just copy this and paste 39 00:02:52,810 --> 00:02:57,520 that down there, and then copy this and paste that down there as well. 40 00:02:58,660 --> 00:03:02,170 Now, the next thing we want to do is we want to go ahead and listen for when our gun is reloading, 41 00:03:02,170 --> 00:03:07,210 and then update the text inside of our gun to say, reloading, and then revert it back to the original 42 00:03:07,210 --> 00:03:10,330 text so we can go to our reload function. 43 00:03:11,050 --> 00:03:15,970 And inside of here, when we're reloading, what we can go ahead and do is. 44 00:03:16,760 --> 00:03:22,160 You can go ahead and refer to our guy, get the frame, and then get the mag ammo text and update the 45 00:03:22,160 --> 00:03:25,730 text inside of it to be equal to something like reloading. 46 00:03:26,150 --> 00:03:31,280 And when the gun is finished reloading, that attribute changed event will automatically fire because 47 00:03:31,280 --> 00:03:35,570 the server will update the attributes inside of our gun, it'll reset the text. 48 00:03:35,570 --> 00:03:42,650 However, we also need to account for the fact that we may not fire to the server to validate our reload 49 00:03:42,650 --> 00:03:49,580 if we unequipped our gun, so we can put an else statement here to check to see, just in case our player 50 00:03:49,580 --> 00:03:55,880 unequipped our gun during the reload animation, and we want to go ahead and update the text back to 51 00:03:55,880 --> 00:03:57,980 the default of mag ammo. 52 00:03:58,600 --> 00:04:03,220 And then we concatenate that with gun get attribute ammo in mag. 53 00:04:03,220 --> 00:04:06,580 And I believe that's all we need to do to implement our GUI. 54 00:04:06,580 --> 00:04:13,840 So we can go ahead and copy our gun from the tools folder, place it inside of our starter pack, and 55 00:04:13,840 --> 00:04:16,810 let's go ahead and see if it's working as we'd expect. 56 00:04:20,180 --> 00:04:21,110 This is why I go and play test. 57 00:04:21,110 --> 00:04:23,930 My game here and I were to equip my gun. 58 00:04:23,930 --> 00:04:24,680 There we go. 59 00:04:24,680 --> 00:04:28,520 It tells us the current ammo in our magazine as well as our reserve ammo. 60 00:04:28,520 --> 00:04:33,890 And then if I were to shoot my gun, as you can see the attribute on our gun updated so it also updated 61 00:04:33,890 --> 00:04:34,550 the text. 62 00:04:34,550 --> 00:04:38,270 And then if I hit R and reload, as you can see it says reloading. 63 00:04:38,910 --> 00:04:43,650 And then both the magazine ammo and the reserve ammo has been updated. 64 00:04:43,650 --> 00:04:44,460 Very cool. 65 00:04:45,280 --> 00:04:51,700 Now, if I shoot and then I reload and I unequip in the middle of my reloading animation, then it should 66 00:04:51,700 --> 00:04:54,580 reset the text to go back to the default. 67 00:04:54,580 --> 00:04:59,830 So if I equip it again, as you can see, we're back to mag ammo and it still says we only have 18 bullets, 68 00:04:59,830 --> 00:05:01,180 but if I reload again. 69 00:05:03,090 --> 00:05:07,410 Now it is updated back to 20 bullets and it removed some from our reserve ammo. 70 00:05:07,410 --> 00:05:08,190 Very cool. 71 00:05:09,660 --> 00:05:11,880 Okay, so we're almost done with our guns. 72 00:05:11,880 --> 00:05:14,580 The last thing we're going to need to do is add hit markers. 73 00:05:14,580 --> 00:05:18,090 When we hit a character model, and our guns will be completed. 74 00:05:18,090 --> 00:05:20,190 I'll see you in the next lecture.